import fetch from "./fetch.js";
const getSong = async (name) => {
    let url = 'https://mojigeci.com/zh-Hans/api/search'
    let body = new URLSearchParams({
        keyword: name
    })
    let res = await fetch(url, {
        method: 'POST',
        headers: {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,sq;q=0.7",
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
            "origin": "https://mojigeci.com",
            "priority": "u=1, i",
            "referer": "https://mojigeci.com/zh-Hans",
            "sec-ch-ua": "\"Not;A=Brand\";v=\"99\", \"Google Chrome\";v=\"139\", \"Chromium\";v=\"139\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
            "x-requested-with": "XMLHttpRequest",
            "Cookie": "PHPSESSID=j9uutj1neufnnko36dfobv8gr8; _ga=GA1.1.16159154.1756191608; _ga_ELLHE1GQ5Y=GS2.1.s1756460722$o9$g1$t1756461118$j58$l0$h0"
        },
        body: body,
        responseType: 'Json',
        signal: controller.signal
    })

    try {
        let data = await res.json()
        if(data.data?.url){
            let res = await fetch('https://mojigeci.com/' + data.data.url)
            let htmlStr = await res.text()
            // 字符串转换 HTML
            let dom = new DOMParser().parseFromString(htmlStr, 'text/html')
            let title = dom.querySelector('h1').textContent
            let audio_url = dom.querySelector('#audio-play-btn').getAttribute('data-audio-url')
            let cover = dom.querySelectorAll('img')[1].getAttribute('src')
            let lyrics = dom.querySelector('.lyrics-content').textContent
            // 相邻节点
            let artist = dom.querySelector('h1').nextElementSibling.textContent
            return {
                title,
                artist,
                cover,
                lyrics,
                audio_url
            }
        }else{
            return data.data
        }
    } catch (err) {
        if (err.name === 'AbortError') {
            return '请求已被取消'
        }
    }
}


const getSongList = async (name) => {
    let params = new URLSearchParams({
        keyword: name,
        page: 1,
        pageSize: 15
    })

    let res = await fetch(`https://mojigeci.com/zh-Hans/api/search_lists?${params.toString()}`, {
        method: 'GET',
        headers: {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,sq;q=0.7",
            "priority": "u=1, i",
            "referer": "https://mojigeci.com/zh-Hans",
            "sec-ch-ua": "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Google Chrome\";v=\"140\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
            "x-requested-with": "XMLHttpRequest",
            "Cookie": "PHPSESSID=j9uutj1neufnnko36dfobv8gr8; _ga=GA1.1.16159154.1756191608; cf_clearance=sPPGkWTrIB6.udDKVkUTuciXmlw3hEK2NC2fbtArfE0-1757639856-1.2.1.1-CqhCFGwlJOTQKDUCvDCxKTz4RKqgNz2iEGlmkdIRXEVBKF3_NGKCG8k8KSOTmeO1zLber1S7KpR0iCLtvwIMM07ZpJAmVg1TTQwyq8QWugJDFGzndO95uJGBF2d4Ts5kvjZgFY6QLhv2S1Ic_DJiDLB.vi6DDJy1nrsD_MryU.7RipXxynmaUEsD6RERxOqcFFpaQBn_Fzn23FNB2VEafsLRVp.i_sU8EuKVI0YVkwo; _ga_ELLHE1GQ5Y=GS2.1.s1757639855$o26$g1$t1757639864$j51$l0$h0"
        }
    })
    try {
        let { data } = await res.json()
        return data.data
    } catch (err) {
        if (err.name === 'AbortError') {
            return '请求已被取消'
        }
    }
}

const getSongDetails = async (id) => {
    let res = await fetch(`https://mojigeci.com/zh-Hans/api/get_lyrics_by_id?id=${id}`, {
        method: 'GET',
        headers: {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,sq;q=0.7",
            "priority": "u=1, i",
            "referer": "https://mojigeci.com/zh-Hans",
            "sec-ch-ua": "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Google Chrome\";v=\"140\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
            "x-requested-with": "XMLHttpRequest",
            "Cookie": "PHPSESSID=j9uutj1neufnnko36dfobv8gr8; _ga=GA1.1.16159154.1756191608; cf_clearance=sPPGkWTrIB6.udDKVkUTuciXmlw3hEK2NC2fbtArfE0-1757639856-1.2.1.1-CqhCFGwlJOTQKDUCvDCxKTz4RKqgNz2iEGlmkdIRXEVBKF3_NGKCG8k8KSOTmeO1zLber1S7KpR0iCLtvwIMM07ZpJAmVg1TTQwyq8QWugJDFGzndO95uJGBF2d4Ts5kvjZgFY6QLhv2S1Ic_DJiDLB.vi6DDJy1nrsD_MryU.7RipXxynmaUEsD6RERxOqcFFpaQBn_Fzn23FNB2VEafsLRVp.i_sU8EuKVI0YVkwo; _ga_ELLHE1GQ5Y=GS2.1.s1757639855$o26$g1$t1757639864$j51$l0$h0"
        }
    })
    try {
        let { data } = await res.json()
        return data.data
    }catch (err) {
        if (err.name === 'AbortError') {
            return '请求已被取消'
        }
    }
}

const getHtmlSong = async (url) => {
    try {
        let res = await fetch('https://mojigeci.com' + url)
        let htmlStr = await res.text()
        // 字符串转换 HTML
        let dom = new DOMParser().parseFromString(htmlStr, 'text/html')
        let name = dom.querySelector('h1').textContent
        let audio_url = dom.querySelector('#audio-play-btn')?.getAttribute('data-audio-url')
        let cover = dom.querySelectorAll('img')[1].getAttribute('src')
        let lyrics = dom.querySelector('.lyrics-content').textContent
        // 相邻节点
        let artist = dom.querySelector('h1').nextElementSibling.innerText
        return {
            name,
            artist,
            cover,
            lyrics,
            audio_url
        }
    }catch (err) {
        console.log(err)
        alert('当前歌曲暂不可播放')
        return '错误'
    }
}

const refreshSongUrl = async (id) => {
    let res = await fetch(`https://mojigeci.com/zh-Hans/api/get_lyrics_by_id?id=${id}&refresh_audio=1`, {
        method: 'GET',
        headers: {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,sq;q=0.7",
            "priority": "u=1, i",
            "referer": "https://mojigeci.com/zh-Hans",
            "sec-ch-ua": "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Google Chrome\";v=\"140\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
            "x-requested-with": "XMLHttpRequest",
            "Cookie": "PHPSESSID=j9uutj1neufnnko36dfobv8gr8; _ga=GA1.1.16159154.1756191608; cf_clearance=sPPGkWTrIB6.udDKVkUTuciXmlw3hEK2NC2fbtArfE0-1757639856-1.2.1.1-CqhCFGwlJOTQKDUCvDCxKTz4RKqgNz2iEGlmkdIRXEVBKF3_NGKCG8k8KSOTmeO1zLber1S7KpR0iCLtvwIMM07ZpJAmVg1TTQwyq8QWugJDFGzndO95uJGBF2d4Ts5kvjZgFY6QLhv2S1Ic_DJiDLB.vi6DDJy1nrsD_MryU.7RipXxynmaUEsD6RERxOqcFFpaQBn_Fzn23FNB2VEafsLRVp.i_sU8EuKVI0YVkwo; _ga_ELLHE1GQ5Y=GS2.1.s1757639855$o26$g1$t1757639864$j51$l0$h0"
        }
    })

    try {
        let { data } = await res.json()
        return data.audio_url
    }catch (err) {
        if (err.name === 'AbortError') {
            return '请求已被取消'
        }
    }
}

export {
    getSong,
    getSongList,
    getSongDetails,
    getHtmlSong,
    refreshSongUrl
}